package com.java_foundation.singleton;

import java.util.concurrent.*;

/**
 * @program: java_foundation
 * @description: 测试
 * @author: xiongbangwen <Email>xiongbangwen@163.com</Email>
 * @create: 2020-05-30 20:41
 **/
public class Test {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        Singleton_1 s1 = Singleton_1.INSTANCE;
//        System.out.println(s1);
//        Singleton_2 s2 = Singleton_2.INSTANCE;
//        System.out.println(s2);
//        Singleton_3 s3 = Singleton_3.INSTANCE;
//        System.out.println(s3);
//        Singleton_4 s4 = Singleton_4.getInstance();
//        Singleton_4 s5 = Singleton_4.getInstance();
//        System.out.println(s4);
//        System.out.println(s5);
        //懒汉式单线程没有问题
//        Callable<Singleton_4> c = new Callable<Singleton_4>() {
//            @Override
//            public Singleton_4 call() throws Exception {
//                return Singleton_4.getInstance();
//            }
//        };
//        ExecutorService es = Executors.newFixedThreadPool(2);
//        Future<Singleton_4> f1 = es.submit(c);
//        Future<Singleton_4> f2 = es.submit(c);
//        Singleton_4 s6 = f1.get();
//        Singleton_4 s7 = f2.get();
//        System.out.println(s6);
//        System.out.println(s7);
//        System.out.println(s6==s7);
        //加锁
//        Callable<Singleton_5> c = new Callable<Singleton_5>() {
//            @Override
//            public Singleton_5 call() throws Exception {
//                return Singleton_5.getInstance();
//            }
//        };
//        ExecutorService es = Executors.newFixedThreadPool(2);
//        Future<Singleton_5> f1 = es.submit(c);
//        Future<Singleton_5> f2 = es.submit(c);
//        Singleton_5 s6 = f1.get();
//        Singleton_5 s7 = f2.get();
//        System.out.println(s6);
//        System.out.println(s7);
//        System.out.println(s6==s7);
        //静态内部类形式
        Callable<Singleton_6> c = new Callable<Singleton_6>() {
            @Override
            public Singleton_6 call() throws Exception {
                return Singleton_6.getInstance();
            }
        };
        ExecutorService es = Executors.newFixedThreadPool(2);
        Future<Singleton_6> f1 = es.submit(c);
        Future<Singleton_6> f2 = es.submit(c);
        Singleton_6 s6 = f1.get();
        Singleton_6 s7 = f2.get();
        System.out.println(s6);
        System.out.println(s7);
        System.out.println(s6==s7);
    }
}
